package com.xk.meeting.dao;

import com.xk.meeting.entity.Apply;
import com.xk.meeting.entityutils.DateTime;
import com.xk.meeting.utils.ConnectionUtils;
import com.xk.meeting.utils.UtilHelper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class ApplyDaoImpl implements ApplyDao{
    private JdbcTemplate template = new JdbcTemplate(ConnectionUtils.getDataSource());
    @Override
    public int addApply(Apply apply) {
        String sql = "insert into apply values(null,?,?,?,?,?,?,?,?)";
        return template.update(sql,apply.getUid(),apply.getMid(), UtilHelper.dateToString(apply.getStarttime()),
                UtilHelper.dateToString(apply.getEndtime()),apply.getTitle(),apply.getApplypeo(),apply.getRemarks(),0);
    }

    @Override
    public List<Apply> getApplys() {
        String sql = "select * from apply";
        return template.query(sql, new BeanPropertyRowMapper<Apply>(Apply.class));
    }

    @Override
    public int getStatus(int id) {
        String sql = "select status from apply where id = ?";
        return template.queryForObject(sql,Integer.class,id);
    }

    @Override
    public int changeStatus(int id, int status) {
        String sql = "update apply set status = ? where id = ?";
        return template.update(sql,status,id);
    }

    @Override
    public Apply getApply(int id) {
        String sql = "select * from apply where id = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Apply>(Apply.class),id);
    }

    @Override
    public List<Integer> getApplyId() {
        String sql = "select id from apply";
        return template.queryForList(sql,Integer.class);
    }

    @Override
    public int isExistTime(String start, String end, String mid) {
        String sql = "select count(id) from apply where not (? > endtime or ? < starttime) and mid = ?";
        return template.queryForObject(sql,Integer.class,start,end,mid);
    }

    @Override
    public List<Apply> getApplys(int mid, DateTime dateTime) {
        String sql = "select * from apply where starttime > ? and endtime < ? and mid = ? and status != -1";
        String weekend = dateTime.getSunday()+ " 23:59:59";
        return template.query(sql,new BeanPropertyRowMapper<Apply>(Apply.class),dateTime.getMonday(),weekend,mid);
    }

    @Override
    public List<Integer> getApplys(int index, int rows) {
        String sql = "select id from apply limit ?,?";
        return template.queryForList(sql,Integer.class,index,rows);
    }

    @Override
    public int getCount() {
        String sql = "select count(id) from apply";
        return template.queryForObject(sql,Integer.class);
    }

    @Override
    public Date getStart(int id) {
        String sql = "select starttime from apply where id = ?";
        return template.queryForObject(sql,Date.class,id);
    }

    @Override
    public Date getEnd(int id) {
        String sql = "select endtime from apply where id = ?";
        return template.queryForObject(sql,Date.class,id);
    }

    @Override
    public int getUid(int id) {
        String sql = "select uid from apply where id = ?";
        return template.queryForObject(sql,Integer.class,id);
    }

    @Override
    public int getMid(int id) {
        String sql = "select mid from apply where id = ?";
        return template.queryForObject(sql,Integer.class,id);
    }
}
